package GuessNumber;

/**
 * @Author mao
 * @Date 2025/11/17
 * @Description 控制游戏运行
 */

class GuessNumPlay {
    // 数字生成线程
    private final NumberGenerator numberGenerator;
    // 数字猜测线程
    private final NumberGuesser numberGuesser;
    // 共享数据类
    private final NumberSync numberSync;

    public GuessNumPlay() {
        numberSync = new NumberSync();
        numberGenerator = new NumberGenerator(numberSync);
        numberGuesser = new NumberGuesser(numberSync);
    }

    public void start() {
        Thread generatorThread = new Thread(numberGenerator);
        Thread guesserThread = new Thread(numberGuesser);

        generatorThread.setName("生成线程");
        guesserThread.setName("猜测线程");

        generatorThread.start();
        guesserThread.start();

        try {
            // 通过join方法等待当前线程结束
            generatorThread.join();
            guesserThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            System.out.println("=== 第 " + (i + 1) + " 轮游戏 ===");
            GuessNumPlay guessNumPlay = new GuessNumPlay();
            guessNumPlay.start();
        }
    }
}
